package com.atguigu.yygh.hosp.api;


import com.atguigu.yygh.common.Result;
import com.atguigu.yygh.common.handler.YyghException;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.hosp.utils.HttpRequestHelper;
import com.atguigu.yygh.hosp.utils.MD5;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 开发医院平台 Controller接口
 */
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    /**
     * 上传医院
     * @param request
     * @return
     */
    @ApiOperation(value = "上传医院")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        //1从request里取出请求参数、转型
        Map<String, String[]> parameterMap = request.getParameterMap();//获取参数映射
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);//转型后的参数
        //2 签名校验
        //2.1 获取相关参数
        String hoscode = (String) paramMap.get("hoscode");
        String sign = (String) paramMap.get("sign");
        //2.2 调取接口（hoscode），查询签名，MD5加密
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        System.out.println("sign = " + sign);
        System.out.println("signKeyMd5 = " + signKeyMd5);
        //2.3 （用密文）校验签名
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(20001, "签名校验失败");
        }
        //2.4 传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);
        //3 调用方法保存数据
        hospitalService.saveHospital(paramMap);
        //4 反馈结果
        return Result.ok();
    }

    /**
     * 获取医院信息
     * @param request
     * @return
     */
    @ApiOperation(value = "获取医院信息")
    @PostMapping("hospital/show")
    public Result hospital(HttpServletRequest request) {//1从request里取出参数、转型
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object>  paramMap = HttpRequestHelper.switchMap(parameterMap);
        //2取出相关参数
        String hoscode =(String)paramMap.get("hoscode");
        String sign =(String)paramMap.get("sign");
        //3签名校验（省略）
        //4调用方法查询医院信息
        Hospital hospital = hospitalService.getHospByHoscode(hoscode); //根据hoscode查询查询医院信息
        //5返回查询结果
        return Result.ok(hospital);
    }

    /**
     * 获取医院信息，医院管理展示多条数据分页用
     */
    /*@ApiOperation(value = "获取医院信息")
    @PostMapping("hospital/show")
    public Result hospital(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();//获取参数映射
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);//转型后的参数
        //取出相关参数
        String hoscode = (String)paramMap.get("hoscode");//主键id
        String sign = (String) paramMap.get("sign");   //密钥
        //2.2 调取接口（hoscode），查询签名，MD5加密
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        System.out.println("sign = " + sign);
        System.out.println("signKeyMd5 = " + signKeyMd5);
        //签名校验
        if(!sign.equals(signKeyMd5)) {
            throw new YyghException(20001, "签名校验失败");
        }
        //2.4 传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);

        //4.封装查询条件，条件验空
        HospitalQueryVo hospitalQueryVo = new HospitalQueryVo();
        hospitalQueryVo.setHoscode(hoscode);  // hoscode主键id
//        hospitalQueryVo.setIsDeleted(0);  // IsDeleted伪删除
        //  isEmpty 判断为是否空，返回Boolean值；   parseInt 将字符串转换成整数；
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 :  //isEmpty如果是空，则赋默认值1（第一页）
                Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : //isEmpty如果是空，则赋默认值10（每页10条）
                Integer.parseInt((String) paramMap.get("limit"));
        //5.调用接口方法，查询医院信息数据
        Page<Hospital> hospitals = hospitalService.selectPageForShow(page, limit, hospitalQueryVo);

        //3 调用方法保存数据
//        Hospital hospital = hospitalService.getByHoscode(hoscode);
        //封装返回
        return Result.ok(hospitals);
    }*/

    /**
     * 上传科室
     * @param request
     * @return
     */
    @ApiOperation(value = "上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        //1.从request里取出参数、转型
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //2.签名校验（省略）
        //3.调用方法上传科室
        departmentService.saveDepartment(paramMap);
        //4.返回结果
        return Result.ok();
    }

    /**
     * 查询科室列表
     * @param request
     * @return
     */
    @ApiOperation(value = "获取科室分页列表")
    @PostMapping("department/list")
    public Result department(HttpServletRequest request) {
        //1.从request里取出参数、转型
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //2.取出相关参数
        String hoscode = (String) paramMap.get("hoscode");//主键id，hoscode = 1000_0
        String sign = (String) paramMap.get("sign");//签名密钥，不做判断用不上了。

        System.out.println("hoscode = " + hoscode);
        //3.签名校验（省略）

        //4.封装查询条件，条件验空
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);  // hoscode主键id
        departmentQueryVo.setIsDeleted(0);  // IsDeleted伪删除
        //  isEmpty 判断为是否空，返回Boolean值；   parseInt 将字符串转换成整数；
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 :  //isEmpty如果是空，则赋默认值1（第一页）
                Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : //isEmpty如果是空，则赋默认值10（每页10条）
                Integer.parseInt((String) paramMap.get("limit"));
        //5.调用接口方法，查询科室信息数据
        Page<Department> pageModel =
                departmentService.selectPage(page, limit, departmentQueryVo);
        //6.封装返回
        return Result.ok(pageModel);
    }

    /**
     * 删除科室
     * @param request
     * @return
     */
    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        //1.从request里取出参数、转型
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //2.获取相关参数
        String hoscode = (String) paramMap.get("hoscode"); //给医院分配的唯一标识。主键id，hoscode = 1000_0
        String depcode = (String) paramMap.get("depcode"); //科室编号。
        String sign = (String) paramMap.get("sign"); //验签参数（签名密钥）。不做判断用不上了

        //3.签名校验（省略）
        //4.调用接口方法删除数据
        departmentService.remove(hoscode, depcode);
        //5.返回结果
        return Result.ok();
    }

    /**
     * 上传排班
     * @param request
     * @return
     */
    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        //1.从request里取出参数、转型
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //2签名校验（省略）
        //3调用方法上传排班
        scheduleService.saveSchedule(paramMap);
        //4返回结果
        return Result.ok();
    }

    /**
     * 查询排班分页列表
     * @param request
     * @return 排班分页列表
     */
    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//        paramMap.forEach();
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        //非必填
        String depcode = (String)paramMap.get("depcode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode); //主键id，给医院分配的唯一标识
        scheduleQueryVo.setDepcode(depcode); //科室编号
        scheduleQueryVo.setIsDeleted(0);//IsDeleted伪删除
        //调用接口方法，查询排班信息数据
        Page<Schedule> pageModel = scheduleService.selectPage(page , limit, scheduleQueryVo);
//        for (Schedule schedule : pageModel) {
//            System.out.println("schedule = " + schedule);
//        }
        //封装返回
        return Result.ok(pageModel);
    }

    /**
     * 删除排班
     * @param request
     * @return
     */
    @ApiOperation(value = "删除排班")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        //必填
        String hosScheduleId = (String)paramMap.get("hosScheduleId");

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }


}
